/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */

#include "shortcutconfig.h"
#include "shortcutwidget.h"
#include "src/platform.h"

#include <QtGui/QTableWidget>
#include <QtGui/QVBoxLayout>
#include <QtCore/QEvent>
#include <QtCore/QSettings>


const int
ShortcutConfig::m_columns = 2;

const QKeySequence
ShortcutConfig::m_default_global_tr =  QKeySequence("Ctrl+Shift+T"),
ShortcutConfig::m_default_local_tr =  QKeySequence("Ctrl+T"),
ShortcutConfig::m_default_swap =  QKeySequence("Ctrl+Shift+S");

ShortcutConfig::ShortcutConfig(QWidget *parent) :
    QWidget(parent)
{
    this->createUi();
    fillTable();
    changeEvent(new QEvent(QEvent::LanguageChange));

    m_settings = new QSettings(Platform::getOrganizationName(),
                               Platform::getApplicationName());
}

ShortcutConfig::~ShortcutConfig() {
    delete tableWidget;
    delete mainLayout;
    delete m_settings;
    delete translateGlobal;
    delete translateLocal;
    delete swapLanguages;
}

void ShortcutConfig::save() {
    m_settings->beginGroup("Shortcuts");
    m_settings->setValue("translateGlobal", translateGlobal->getShortcut().toString());
    m_settings->setValue("translateLocal", translateLocal->getShortcut().toString());
    m_settings->setValue("swapLanguages", swapLanguages->getShortcut().toString());
    m_settings->endGroup();

}

void ShortcutConfig::read() {
    m_settings->beginGroup("Shortcuts");
    translateGlobal->setShortcut(m_settings->value("translateGlobal", m_default_global_tr).toString());
    translateLocal->setShortcut(m_settings->value("translateLocal", m_default_local_tr).toString());
    swapLanguages->setShortcut(m_settings->value("swapLanguages", m_default_swap).toString());
    m_settings->endGroup();
}

void ShortcutConfig::truncate() {
    translateGlobal->setShortcut(m_default_global_tr);
    translateLocal->setShortcut(m_default_local_tr);
    swapLanguages->setShortcut(m_default_swap);
}

QKeySequence ShortcutConfig::getGlobalTranslateShortcut() const {
    return translateGlobal->getShortcut();
}

QKeySequence ShortcutConfig::getLocalTranslateShortcut() const {
    return translateLocal->getShortcut();
}

QKeySequence ShortcutConfig::getSwapShortcut() const {
    return swapLanguages->getShortcut();
}

void ShortcutConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
        tableWidget->itemAt(0,0)->setText(QWidget::tr("Translate Selected"));
        tableWidget->itemAt(1,0)->setText(QWidget::tr("Translate "));
        tableWidget->itemAt(2,0)->setText(QWidget::tr("Swap Languages"));
    }
}

void ShortcutConfig::createUi() {
    tableWidget = new QTableWidget(this);
    tableWidget->setColumnCount(m_columns);
    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    mainLayout = new QVBoxLayout(this);

    mainLayout->addWidget(tableWidget);
    this->setLayout(mainLayout);
}

void ShortcutConfig::fillTable() {

    translateGlobal = new ShortcutWidget(this);
    translateLocal  = new ShortcutWidget(this);
    swapLanguages = new ShortcutWidget(this);

    this->insertShortcut(/*QWidget::tr("Translate Selected"),*/ translateGlobal);
    this->insertShortcut(/*QWidget::tr("Translate"), */translateLocal);
    this->insertShortcut(/*QWidget::tr("Swap Languages"),*/ swapLanguages);
}


void ShortcutConfig::insertShortcut(/*const QString &txt,*/
                                     ShortcutWidget *widget) {

    const int cur_row = tableWidget->rowCount() + 1;
    tableWidget->setRowCount(cur_row);

    QTableWidgetItem *l = new QTableWidgetItem;


    tableWidget->setItem(cur_row - 1,0,l);
    tableWidget->setCellWidget(cur_row -1,1,widget);

}
